package tool

import models.Tables._

import java.io.File
import implicits.Implicits._
import org.joda.time.DateTime
import play.api.mvc.RequestHeader

/**
 * Created by yz on 24/9/2021
 */
trait MissionTool {

  def getMissionWorkspaceDir(mission: MissionRow) = {
    val missionIdDir = getMissionIdDir(mission)
    new File(missionIdDir, "workspace")
  }

  def getMissionWorkspaceDir(id: Int)(implicit request: RequestHeader) = {
    val missionIdDir = getMissionIdDir(id)
    new File(missionIdDir, "workspace")
  }

  def getMissionIdDir(mission: MissionRow) = {
    val userMissionFile = getUserMissionDir(mission.userId)
    new File(userMissionFile, mission.id.toString)
  }

  def getUserMissionDir(userId: Int) = {
    val userIdDir = Tool.getUserIdDir(userId)
    new File(userIdDir, "mission")
  }

  def getInitLogFile(dir: File) = {
    val file = new File(dir, "log.txt")
    "Run successfully!".toFile(file)
    file
  }

  def getLogFile(dir:File)={
    new File(dir, "log.txt")
  }

  def getUserMissionDir(implicit request: RequestHeader) = {
    val userIdDir = Tool.getUserIdDir
    new File(userIdDir, "mission")
  }

  def generateMissionName = {
    (new DateTime).toString("yyyyMMddHHmmss")
  }

  def getMissionResultDir(mission: MissionRow) = {
    val missionIdDir = getMissionIdDir(mission)
    new File(missionIdDir, "result")
  }

  def getMissionIdDir(missionId: Int)(implicit request: RequestHeader) = {
    val userMissionFile = getUserMissionDir
    new File(userMissionFile, missionId.toString)
  }

  def getConfigFile(workspaceDir: File) = {
    new File(workspaceDir, "config.json")
  }

}
